import axios from 'axios';
import {message,Modal} from 'antd';
const { confirm } = Modal;
let http = {};
const mianurl = '/pipe/';
// const mianurl = 'http://192.168.1.200:8000/pipe/';
// const mianurl = 'http://39.98.168.240:8000/pipe/';
const token = localStorage.getItem('token');
http.get =function getters(url){
    axios.defaults.headers.common['token'] = localStorage.getItem('token');
    return new Promise(function (resolve,reject) {
        axios.get(mianurl + url)
        .then(function (res) {
            if(res.data === 401){
                window.location.href='/login';
            }else if(res.data.success){
                resolve(res.data);
            }else{
                message.error(res.data.msg);
                reject(res.data)
            }
        })
        .catch(function (res) {
                message.error("请求失败")
                reject(res)
        });
    })
}
http.delete =function delet(url,handleCode){
    axios.defaults.headers.common['token'] = token;
    axios.defaults.headers.common['handleCode'] = handleCode;
    return new Promise(function (resolve,reject) {
        axios.delete(mianurl+ url)
        .then(function (res) {
            if(res.data.code === 0){
                message.error(res.data);
                resolve(res.data);
            }else{
                message.error(res.data);
                reject(res.data)
            }
        })
        .catch(function () {
            reject('请求失败')
        });
    })
}
http.post =(url,data,handleCode,requstType)=>{
    axios.defaults.headers.common['handleCode'] = handleCode
    axios.defaults.headers.common['token'] = token;
    axios.defaults.headers.common['Content-Type'] = requstType;
    return new Promise(function (resolve,reject) {
        axios.post(mianurl + url,data,{headers:{'Content-Type': requstType}})
        .then(function (res) {
            if(res.data.success){
                resolve(res.data);
            }else{
                message.error('请求失败,检查操作');
                reject('请求失败')
            }
        })
        .catch(function () {
            message.error('请求失败,检查操作');
            reject('请求失败')
        });
    })
}
http.axiosapi=(way,url,data,handleCode)=>{
    axios.defaults.headers.common['handleCode'] = handleCode
    axios.defaults.headers.common['token'] = token;
    return new Promise(function (resolve,reject) {
        axios({
            method: way,
            url: url,
            data: data
        }).then(function (res) {
            if(res.data.code === 0){
                resolve(res.data);
            }else{
                message.error(res);
                reject(res.data)
            }
        })
        .catch(function () {
            message.error('请求失败,检查操作');
            reject('请求失败');
        });;
    })
}
http.put =(url,data,handleCode)=>{
    axios.defaults.headers.common['handleCode'] = handleCode
    axios.defaults.headers.common['token'] = token;
    return new Promise(function (resolve,reject) {
        axios.put(mianurl+ url,data)
        .then(function (res) {
            if(res.data.code === 0){
                message.success('删除成功');
                resolve(res.data);
            }else{
                message.error(res);
                reject(res.data)
            }
        })
        .catch(function () {
            message.error('请求失败,检查操作');
            reject('请求失败')
        });
    })
}
http.delectConfirm =(url,handleCode)=>{
    return new Promise(function (resolve,reject) {
        confirm({
            title: '是否真的要删除',content: '数据一旦删除，将不可恢复，请慎重操作',okText: '删除',okType: 'danger', cancelText: '取消',
            onOk(){
                axios.defaults.headers.common['token'] = token;
                axios.defaults.headers.common['handleCode'] = handleCode
                axios.delete(mianurl+ url)
                .then(function (res) {
                    console.log(res)
                    if(res.data.code === 0||res.data.code === 200){
                        message.success('删除成功');
                        resolve(res.data);
                    }else{
                        message.error(res.data);
                        reject(res.data)
                    }
                })
                .catch(function () {
                    message.error('请求失败,检查操作');
                    reject('请求失败')
                });
            }
        });
    })
}
export default http;
